import Http from './utils/Http'
import Cache from './utils/Cache'
import url from './utils/url'
const http = new Http;

App({
  onLaunch() {
    // 判断缓存中是否存在openid,如果存在则不需要再次进行网络请求
    if (!Cache.has('openid')) {
      wx.login({
        timeout: 10000,
        success: (result) => {
          let code = result.code;
          http.httpRequest({
            url: url.wxlogin,
            data: { code },
            method: 'POST'
          }).then(ret => {
            // 得到返回过来的openid
            let openid = ret.data.data.openid;
            // 数据库中的记录id主键
            let id = ret.data.data.id
            // 写入缓存中
            Cache.forever('openid', openid);
            Cache.forever('id',id)
          })
        }
      });
    }
  }
})